﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework;
using System.Data;

namespace DomMgmtSystem.View
{
    public partial class InHSRegister : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal TB_HSYXRK_BASE_ID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["baseID"]))
                    return Convert.ToDecimal(Request["baseID"]);
                return 0;
            }
        }

        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }

        }

        #endregion

        public string rows = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBind();

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();imghide();</script>");
        }

        protected void btnInHSRegisterSave_Click(object sender, EventArgs e)
        {
            List<Model.TB_HSYXRK_HD> hds = new List<Model.TB_HSYXRK_HD>();
            if (txthdlist.Text.Trim() != "")
                hds = JsonHelper.ParseFormJson<List<Model.TB_HSYXRK_HD>>(txthdlist.Text);
            Model.TB_HSYXRK_BASE hsBase = new Model.TB_HSYXRK_BASE
            {
                CBZ = txtCBZ.Text.Trim(),
                CHSDW = txtCHSDW.Text.Trim(),
                CHSYLX = txtCHSYLX.Text.Trim(),
                CSMDW = txtCSMDW.Text.Trim(),
                CSMFBL = txtCSMFBL.Text.Trim(),
                CSQDH = dlCSQDH.SelectedItem.Text,
                CSQMC = lbCSQMC.Text.Trim(),
                DTJSRQ = Convert.ToDateTime(txtDTJSRQ.Text.Trim()),
                DTRKRQ = Convert.ToDateTime(txtDTRKRQ.Text.Trim()),
                CREATEUSER = Session["userID"].ToString(),
                NYPS = string.IsNullOrEmpty(txtNYPS.Text.Trim()) ? 0 : Convert.ToDecimal(txtNYPS.Text.Trim()),
                NYPTS = string.IsNullOrEmpty(ddlNYPTS.Text.Trim()) ? 0 : Convert.ToDecimal(ddlNYPTS.SelectedValue)
            };
            if (TB_HSYXRK_BASE_ID != 0)
            {
                hsBase.NID = TB_HSYXRK_BASE_ID;
                DataAccess.TB_HSYXRK_BASE.Update(hsBase);

                DataAccess.TB_HSYXRK_HD.DeleteList(" NSQID=" + hsBase.NID);
            }
            else
            {
                hsBase = DataAccess.TB_HSYXRK_BASE.Add(hsBase);
                Model.TB_CHECK check = DataAccess.TB_CHECK.GetModel(decimal.Parse(dlCSQDH.SelectedItem.Value.Split('ξ')[0]));
                check.STATE = 1;
                DataAccess.TB_CHECK.Update(check);
            }

            foreach (Model.TB_HSYXRK_HD item in hds)
            {
                item.NSQID = hsBase.NID;
                if (string.IsNullOrEmpty(item.NJZBH))
                    item.NJZBH = DataAccess.CommonExecute.GetBarCode("1", "1");
                DataAccess.TB_HSYXRK_HD.Add(item);
            }
            if (TB_HSYXRK_BASE_ID != 0)
                this.RedirectAfterInfo("编辑成功！", "HSYXRK_BASE_List.aspx");
            else
                this.RedirectConfirmInfo("新增成功！是否继续新增？", "InHSRegister.aspx", "HSYXRK_BASE_List.aspx");
        }
        public void DataBind()
        {
            DataTable dt = null;
            if (TB_HSYXRK_BASE_ID != 0)
            {
                btnBack.Visible = true;
                lbTitle.Text = IsView ? "查看航摄影像入库基本信息" : "编辑航摄影像入库基本信息";
                Model.TB_HSYXRK_BASE hsBase = DataAccess.TB_HSYXRK_BASE.GetModel(TB_HSYXRK_BASE_ID);
                dt = DataAccess.TB_CHECK.GetList(" SQDH='" + hsBase.CSQDH + "' ").Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    dlCSQDH.Items.Add(new ListItem(item["SQDH"].ToString(), item["NID"].ToString() + "ξ" + item["CSQMC"].ToString()));
                    lbCSQDH.Text = item["SQDH"].ToString();
                }
                lbCSQDH.Visible = true;
                //dlCSQDH.Style.Add("display", "none");
                dlCSQDH.Visible=false;
                txtCBZ.Text = hsBase.CBZ;
                txtCHSDW.Text = hsBase.CHSDW;
                txtCHSYLX.Text = hsBase.CHSYLX;
                txtCSMDW.Text = hsBase.CSMDW;
                txtCSMFBL.Text = hsBase.CSMFBL;
                lbCSQMC.Text = hsBase.CSQMC;
                txtDTJSRQ.Text = hsBase.DTJSRQ.Value.ToString("yyyy-MM-dd");
                txtDTRKRQ.Text = hsBase.DTRKRQ.Value.ToString("yyyy-MM-dd");
                txtNYPS.Text = hsBase.NYPS.ToString();
                ddlNYPTS.SelectedValue = hsBase.NYPTS.ToString();

                dt = DataAccess.TB_HSYXRK_HD.GetList(" NSQID=" + TB_HSYXRK_BASE_ID).Tables[0];

                string nypts = "";
                string ncfch = "<option value='{0}'>{0}</option>";
                string ncfSelect = "<option value='{0}' selected='selected'>{0}</option>";
                int num = 1;
                foreach (DataRow item in dt.Rows)
                {
                    if (item["NYPTS"].ToString() == "1")
                        nypts = "  <option value='1' selected='selected'>第一套</option><option value='2'>第二套</option>";
                    else
                        nypts = "  <option value='1'>第一套</option><option value='2' selected='selected'>第二套</option>";
                    ncfch = string.Format("<option value='{0}'>{0}</option>", item["NCFCH"]);
                    ncfSelect = string.Format("<option value='{0}' selected='selected'>{0}</option>", item["NCFCH"]);
                    rows += string.Format(tr, item["NID"], nypts, item["CHSBLC"], item["NYPXH"], item["CSN"], item["FSJL"], item["NSMPS"], item["FSLRGB"], item["CNR"], item["NCFGH"], item["NCFLH"], num, item["NJZBH"]).Replace(ncfch, ncfSelect);
                    num++;
                }
            }
            else
            {
                lbTitle.Text = "新增航摄影像入库基本信息";
                dt = DataAccess.TB_CHECK.GetList(" STATE=0 ").Tables[0];
                dlCSQDH.Items.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    dlCSQDH.Items.Add(new ListItem(item["SQDH"].ToString(), item["NID"].ToString() + "ξ" + item["CSQMC"].ToString()));
                }
                dlCSQDH.DataBind();
                if (dt.Rows.Count > 0)
                    lbCSQMC.Text = dlCSQDH.Items[0].Value.Split('ξ')[1];
                rows = "<TR><TD style='DISPLAY: none'><INPUT name='NID' type='text' fieldtype='decimal'/> <INPUT name='NJZBH' type='text' fieldtype='decimal'/></TD><TD><SELECT name='NYPTS' fieldtype='decimal'> <OPTION value='1' selected>第一套</OPTION> <OPTION value='2'>第二套</OPTION></SELECT> </TD><TD><INPUT name='CHSBLC' style='WIDTH: 80px' type='text' fieldtype='nvarchar'> </TD><TD><INPUT name='NYPXH' style='WIDTH: 80px' type='text' fieldtype='nvarchar'> </TD><TD><INPUT name='CSN' style='WIDTH: 80px' type='text' fieldtype='nvarchar'> </TD><TD><INPUT name='FSJL' style='WIDTH: 50px' onkeyup=clearNoNum(this) type='text' fieldtype='decimal'> </TD><TD><INPUT name='NSMPS' style='WIDTH: 50px' onkeyup=clearNoNum(this) type='text' fieldtype='decimal'> </TD><TD><INPUT name='FSLRGB' style='WIDTH: 50px' onkeyup=clearNoNum(this) type='text' fieldtype='decimal'> </TD><TD><INPUT name='CNR' style='WIDTH: 80px' type='text' fieldtype='nvarchar'> </TD><TD><INPUT name='NCFGH' style='WIDTH: 80px' onkeyup=clearNoNum(this) type='text' fieldtype='decimal' isnull='False'> </TD><TD><SELECT name='NCFCH' fieldtype='decimal'> <OPTION value=1 selected>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION value=9>9</OPTION></SELECT> </TD><TD><INPUT name='NCFLH' style='WIDTH: 80px' onkeyup=clearNoNum(this) type='text' fieldtype='decimal' isnull='False'> </TD><TD width=20 align=center><A id='img1' onclick='javascript:delrow(this)' style='cursor: pointer'><IMG style='BORDER-TOP: 0px; BORDER-RIGHT: 0px; RIGHT: 5px; BORDER-BOTTOM: 0px; FLOAT: right; BORDER-LEFT: 0px' src='../images/del.gif'></A> </TD></TR>";
            }
        }
        #region
        string tr = @"<tr>
<td style='display: none'>
    <input name='NID' fieldtype='decimal'  value='{0}'>
<INPUT name='NJZBH' type='text' fieldtype='decimal'  value='{12}'/>
</td>
<td>
    <select name='NYPTS' fieldtype='decimal'>
       {1}
    </select>
</td>
<td>
    <input name='CHSBLC' style='width: 80px' type='text' fieldtype='nvarchar' 
        value='{2}'>
</td>
<td>
    <input name='NYPXH' style='width: 80px' type='text' fieldtype='nvarchar' 
        value={3}>
</td>
<td>
    <input name='CSN' style='width: 80px' type='text' fieldtype='nvarchar' 
        value={4}>
</td>
<td>
    <input name='FSJL' style='width: 50px' type='text' fieldtype='decimal' 
        onkeyup='clearNoNum(this)' value={5}>
</td>
<td>
    <input name='NSMPS' style='width: 50px' type='text' fieldtype='decimal' 
        onkeyup='clearNoNum(this)' value={6}>
</td>
<td>
    <input name='FSLRGB' style='width: 50px' type='text' fieldtype='decimal' 
        onkeyup='clearNoNum(this)' value={7}>
</td>
<td>
    <input name='CNR' style='width: 80px' type='text' fieldtype='nvarchar' 
        value={8}>
</td>
<td>
    <input name='NCFGH' style='width: 80px' type='text' fieldtype='decimal' 
        onkeyup='clearNoNum(this)' value={9} isnull='False'>
</td>
<td>
    <select name='NCFCH' fieldtype='decimal'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
        <option value='4'>4</option>
        <option value='5'>5</option>
        <option value='6'>6</option>
        <option value='7'>7</option>
        <option value='8'>8</option>
        <option value='9'>9</option>
    </select>
</td>
<td>
    <input name='NCFLH' style='width: 80px' type='text' fieldtype='decimal' 
        onkeyup='clearNoNum(this)' value={10} isnull='False'>
</td>
<td width='20' align='center'>
    <a style='cursor: pointer' onclick='javascript:delrow(this)' id='img{11}'>
        <img style='border-top: 0px; border-right: 0px; right: 5px; border-bottom: 0px; float: right;
            border-left: 0px' src='../images/del.gif' /></a>
</td>
</tr>";
        #endregion

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("HSYXRK_BASE_List.aspx");
        }
    }
}